AddCSLuaFile("cl_init.lua")
AddCSLuaFile("shared.lua")

include("shared.lua")

function ENT:Initialize()

	self:SetModel( "models/props_c17/canister_propane01a.mdl" )
	
	self:PhysicsInit(SOLID_VPHYSICS)
	self:SetMoveType( MOVETYPE_VPHYSICS )
	self:SetSolid( SOLID_VPHYSICS ) 
	
	self:GetPhysicsObject():EnableMotion(false)
	
	self:SetUseType(SIMPLE_USE)
	
	self:SetColor(0,0,0,1)
	
	self.prop = ents.Create("prop_dynamic")
	self.prop:SetPos(self:GetPos() + Vector(0,0,-5))
	self.prop:SetAngles(self:GetAngles())
	self.prop:SetModel(self.model)
	self.prop:Spawn()
	self.prop:Activate()
	
	self.prop:SetParent(self)
	
	local seq = self.prop:LookupSequence("idle01")
	if seq == -1 then
		seq = self.prop:LookupSequence("idle")
	end
	if seq == -1 then
		seq = self.prop:LookupSequence("idle_subtle")
	end
	
	self.prop:SetSequence(seq)
	
	timer.Create( "BuyMoar" .. self:EntIndex(), 60, 0, self.IncreaseBuyAmt, self )
	
	local tab = buyers[self.buytype]
	
	self:SetDTInt(0, tab.buyamt)
	
	timer.Simple(1, function()
		umsg.Start("buyerinfo")
			umsg.Short(self:EntIndex())
			umsg.String(self.name)
			umsg.String(self.buytype)
		umsg.End()
	end)

end

function ENT:Use( ply )

	--do stuff

end

function ENT:IncreaseBuyAmt()
	local tab = buyers[self.buytype]
	if tab.flux > 0 then
		self:SetDTInt(0, math.Clamp(self:GetDTInt(0) + tab.flux, tab.min, tab.max))
		print("buy amount incresed to" .. self:GetDTInt(0))
	end
end

function ENT:OnRemove()
	timer.Remove("BuyMoar" .. self:EntIndex())
end

function ENT:StartTouch( ent )
	if buyers[self.buytype] == nil then
		ErrorNoHalt("Incorrect buytype! Removing...")
		self:Remove()
	end
	
	if ent.picker == nil then
		return
	end
	
	local tab = buyers[self.buytype]
	
	if ent:GetClass() == tab.entclass then
		if tab.requiredmodel and ent:GetModel() != tab.requiredmodel then
			return
		end
		
		if tab.requiredvars then
			for k,v in pairs(tab.requiredvars) do
				if ent[k] != v then 
					return
				end
			end
		end
		
		-- so it passed the checks time to sell it!
		ent.picker:AddMoney(self:GetDTInt(0))
		ent.picker:ColChat("DrpNPC", Color(0,255,0), "Your item has been sold for " .. self:GetDTInt(0) .. "$!")
		
		if tab.flux > 0 then
			self:SetDTInt(0, math.Clamp(self:GetDTInt(0) - tab.flux, tab.min, tab.max))
		end
		ent:Remove()

		self:EmitSound(Sound("Buttons.snd9"))
	end
end